#ifndef __MAIN_H__
#define __MAIN_H__

#include "webglk/glk.h"
#include <stdio.h>

typedef struct {
	uint8_t type;
	uint32_t version;

	uint32_t romSize;
	uint32_t stackSize;

	uint32_t ram;
	uint8_t iosys;

	uint32_t event;
	uint32_t inputbuffer;

	int32_t randa,randi,randc;

	uint16_t extensions,numExtensions;
	uint16_t unicode;

	uint16_t tokens;
	uint32_t timer;
	uint16_t timerfunc;

	int8_t screen,memory;
	strid_t mems[20];
	uint16_t memlens[20];

	int curwin;
	winid_t wins[8];
	uint16_t cursorx,cursory;

	uint32_t pc,sp,fp,lp;
} Variables;

extern Variables g;
extern uint8_t *rom;
extern uint8_t *stack;
extern int done;

extern uint32_t screenwidth,screenheight,font3width,font3height,font4width,font4height;

void loadROM(uint8_t);
void z_init();
void g_init();
void z_run();
void g_run();
void z_select(event_t *);
void g_select(event_t *);

void webglk_open();
void webglk_close();
void webglk_load(FILE *);
void webglk_save(FILE *);
void glk_stream_buffer_flush();
void fatal(const char *fmt,...);

void printNum(int32_t);
uint8_t save(uint32_t,uint32_t,uint32_t,uint32_t);
uint8_t load(uint32_t,uint32_t,uint32_t *,uint32_t *);

#define read32(x) ((rom[x]<<24)|(rom[(x)+1]<<16)|(rom[(x)+2]<<8)|rom[(x)+3])
#define read16(x) ((rom[x]<<8)|rom[(x)+1])
void write32(uint32_t,uint32_t);
void write16(uint32_t,uint16_t);

void push(uint32_t);
uint32_t pop();
void poke(uint32_t,uint32_t);
#define peek(x) ((stack[x]<<24)|(stack[(x)+1]<<16)|(stack[(x)+2]<<8)|stack[(x)+3])

#endif
